import http from '../../utils/http';
import * as types from '../mutations-type'
import md5 from "js-md5"

const state = {
    sid: '',
    addressIsReturn: false,
    setAddressInfo: {},
    userInfo: {}
};

const actions = {

    //用户注册
    [types.USER_REGISTER]({dispatch}, userInfo) {
        return new Promise((resolve) => {
            http.post('/user.register', userInfo).then((res) => {
                return resolve(res.data);
            });
        });
    },
    //用户登录
    [types.USER_LOGIN]({commit, dispatch}, userInfo) {
        return new Promise((resolve) => {
            http.post('/user.login', userInfo).then((res) => {
                if (res.data.code == 0) {
                    commit(types.SET_USER_SID, res.data.sid)
                    dispatch(types.CART_NUM)
                    dispatch(types.USER_INFO)
                }
                return resolve(res.data);
            });
        });
    },
    //通过手机找回密码
    [types.USER_FIND_PASSWORD]({}, userInfo) {
        return new Promise((resolve) => {
            http.post('/user.findPass', userInfo).then((res) => {
                return resolve(res.data);
            });
        });
    },
    //用户修改密码
    [types.USER_UPDATE_PASSWORD]({commit, dispatch}, userInfo) {
        return new Promise((resolve) => {
            http.post('/user/pwd.change', userInfo).then((res) => {
                if (res.data.code == 0) {
                    commit(types.SET_USER_SID, '')
                    commit(types.SET_CART_NUM, 0)
                    commit(types.SET_USER_INFO, {})

                }
                return resolve(res.data);
            });
        });
    },

    //用户修改密码
    [types.USER_UPDATE_PASSWORD2]({commit, dispatch}, userInfo) {
        return new Promise((resolve) => {
            http.post('/user/pwd2.change', userInfo).then((res) => {
                if (res.data.code == 0) {
                    dispatch(types.USER_INFO)
                }
                return resolve(res.data);
            });
        });
    },

    //获取用户信息
    [types.USER_INFO]({commit}) {
        return new Promise((resolve) => {
            http.post('/user/info.get').then((res) => {
                console.log(res)
                if (res.data.code == 0) {
                    commit(types.SET_USER_INFO, res.data.data)
                }
                return resolve(res.data);
            });
        });
    },
    //编辑用户信息
    [types.USER_INFO_EDIT]({dispatch, commit}, form) {
        form.pwd2 = md5(form.pwd2)
        return new Promise((resolve) => {
            http.post('/user/info.set', form).then((res) => {
                console.log(res)
                if (res.data.code == 0) {
                    dispatch(types.USER_INFO)
                }
                return resolve(res.data);
            });
        });
    },
    //退出登录
    [types.USER_LAYOUT]({commit}) {
        commit(types.SET_USER_SID, '')
        commit(types.SET_USER_INFO, {})
        commit(types.SET_CART_NUM, 0)
    }

};


const mutations = {

    [types.SET_USER_SID](state, sid) {
        state.sid = sid;
        window.localStorage.setItem('sid', sid)
    },
    //记录路由是否返回
    [types.SET_ADDRESS_RECORD](state, val) {
        state.addressIsReturn = val
    },
    [types.SET_ADDRESS_CHOICE](state, addressInfo) {
        state.setAddressInfo = addressInfo
    },
    //储存用户信息
    [types.SET_USER_INFO](state, userInfo) {
        state.userInfo = userInfo
        window.localStorage.setItem('userInfo', JSON.stringify(userInfo))
    },
};


export default {
    state,
    actions,
    mutations,
    getters: {
        sid(state) {
            if (!state.sid && window.localStorage) state.sid = window.localStorage.getItem("sid");
            return state.sid ? state.sid : ''
        },
        addressIsReturn: state => state.addressIsReturn,
        setAddressInfo: state => state.setAddressInfo,
        userInfo(state) {
            if (!state.userInfo.real_name) state.userInfo = JSON.parse(window.localStorage.getItem("userInfo"));
            return state.userInfo ? state.userInfo : ''
        }
    }
}
